package com.zl.common.thread;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.zl.weixin.entity.AccessToken;
import com.zl.weixin.util.WeixinUtil;

public class TokenThread implements Runnable {  
    private static Logger log = LoggerFactory.getLogger(TokenThread.class);  
    // 第三方用户唯一凭证  
    public static String appid = "wxac9a290c5dc49ed0";  
    // 第三方用户唯一凭证密钥  
    public static String appsecret = "24dd8e15cfeb82fc1c0ac60c8705d75c";  
    public static AccessToken accessToken = null;  
  
    public void run() {  
        while (true) {  
            try {  
                accessToken = WeixinUtil.getAccessToken(appid, appsecret);  
                System.out.println(accessToken.getToken());
                if (null != accessToken) {  
                    log.info("获取access_token成功，有效时长{}秒 token:{}", accessToken.getExpiresIn(), accessToken.getToken()); 
                    //System.out.println("获取access_token成功，有效时长{}秒 token:{}");
                    // 休眠1800秒  
                    Thread.sleep((accessToken.getExpiresIn() - 5400) * 1000);  
                } else {  
                    // 如果access_token为null，60秒后再获取  
                    Thread.sleep(60 * 1000);  
                }  
            } catch (InterruptedException e) {  
                try {  
                    Thread.sleep(60 * 1000);  
                } catch (InterruptedException e1) {  
                    log.error("{}", e1);  
                }  
                log.error("{}", e);  
            }  
        }  
    }  
    
    
}  
